#include "workerManager.h"

workerManager::workerManager()
{
    //1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in); //读文件

    if (!ifs.is_open())
    {
        cout << "文件不存在" << endl;
        //初始化记录人数
        this->m_EmpNum = 0;

        //初始化数组指针
        this->m_empArray = NULL;

        //初始化文件是否为空
        this->m_FileIsEmpty = true;
    }

    //2.文件存在  数据为空
    char ch;
    ifs >> ch;
    if (ifs.eof()) //判断文件是否为空
    {
        //文件为空
        cout << "文件为空!" << endl;

        //初始化记录人数
        this->m_EmpNum = 0;

        //初始化数组指针
        this->m_empArray = NULL;

        //初始化文件是否为空
        this->m_FileIsEmpty = true;

        ifs.close();
        return;
    }

    //如果文件存在,且有数据记录
    int num = this->get_EmpNum();

    //将文件中的信息放入指针数组中
    this->m_EmpNum = num;
    //开辟空间
    this->m_empArray = new Worker *[this->m_EmpNum];
    //将职工信息加入数组中
    this->init_Emp();
    this->m_FileIsEmpty = false;
}

void workerManager::Show_Menu()
{
    cout << "***********************************************" << endl;
    cout << "***********  欢迎使用职工管理系统！ ***********" << endl;
    cout << "***********      1.增加职工信息     ***********" << endl;
    cout << "***********      2.显示职工信息     ***********" << endl;
    cout << "***********      3.删除职工信息     ***********" << endl;
    cout << "***********      4.修改职工信息     ***********" << endl;
    cout << "***********      5.查找职工信息     ***********" << endl;
    cout << "***********      6.按照编号排序     ***********" << endl;
    cout << "***********      7.清空所有文档     ***********" << endl;
    cout << "***********      0.退出管理系统     ***********" << endl;
    cout << endl;
}

void workerManager::ExitSystem()
{
    cout << "欢迎下次使用" << endl;
    exit(0); //退出系统  正常退出
}

void workerManager::Add_emp()
{
    cout << "请输入想要添加的员工数量" << endl;
    int addNum = 0;
    cin >> addNum;

    if (addNum > 0)
    {
        //计算添加新空间大小
        int newSize = this->m_EmpNum + addNum; //新开辟空间的大小,等于原人数+ 新人数
        //新开辟空间存放职工信息的指针
        Worker **newSpace = new Worker *[newSize];
        //将原来空间的数据拷贝到新的空间
        if (this->m_empArray != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                newSpace[i] = this->m_empArray[i];
            }
        }
        //批量添加新数据
        for (int i = 0; i < addNum; i++)
        {
            int id;      //职工编号
            string name; //职工姓名
            int dSelect; //部门选项

            cout << "请输入第" << i + 1 << "个新职工编号" << endl;
            cin >> id;

            cout << "请输入第" << i + 1 << "个新职工姓名" << endl;
            cin >> name;

            Worker *worker = NULL;
            int flag = 1;
            while (flag)
            {
                cout << "请选择该职工岗位：" << endl;
                cout << "1.普通员工" << endl;
                cout << "2.经理" << endl;
                cout << "3.总裁" << endl;
                cin >> dSelect;
                switch (dSelect)
                {
                case 1:
                    worker = new Employee(id, name, dSelect);
                    flag = 0;
                    break;
                case 2:
                    worker = new Manager(id, name, dSelect);
                    flag = 0;
                    break;
                case 3:
                    worker = new Boss(id, name, dSelect);
                    flag = 0;
                    break;
                default:
                    cout << "选择错误,重新选择:" << endl;
                    break;
                }
            }
            //将职工信息保存到职工数组中
            newSpace[this->m_EmpNum + i] = worker;
        }

        //释放原有空间
        delete[] this->m_empArray;

        //更改新空间的指向
        this->m_empArray = newSpace;

        //更新新的职工数量
        this->m_EmpNum = newSize;

        //更新文件不为空标志
        this->m_FileIsEmpty = false;

        //成功添加后保存到文件中
        this->save();

        //提示添加成功
        cout << "成功添加" << addNum << "名新职工" << endl;
    }
    else
    {
        cout << "输入数据有误,请重新输入" << endl;
    }
    return;
}

void workerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out); //以输出的方式打开文件--写文件
    if (!ofs.is_open())
    {
        cout << "打开文件失败" << endl;
        return;
    }
    //将每个人的数据写入文件中
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        ofs << this->m_empArray[i]->m_ID << " "
            << this->m_empArray[i]->m_Name << " "
            << this->m_empArray[i]->m_DepID << endl;
    }
    ofs.close();
}

//统计文价中的员工个数
int workerManager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in); //读文件

    int id;
    string name;
    int dId;
    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        //统计人数
        num++;
    }
    ifs.close();
    return num;
}

//根据文件信息初始化职工数组
void workerManager::init_Emp()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int dId;
    int index = 0;
    //读取文件信息
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        Worker *worker = NULL;
        if (dId == 1) //普通员工
        {
            worker = new Employee(id, name, dId);
        }
        else if (dId == 2)
        {
            worker = new Manager(id, name, dId);
        }
        else
        {
            worker = new Boss(id, name, dId);
        }
        this->m_empArray[index] = worker;
        index++;
    }
    //关闭文件
    ifs.close();
}

void workerManager::show_Emp()
{
    if (this->m_FileIsEmpty == true)
    {
        cout << "文件不存在或者记录为空" << endl;
        return;
    }
    else
    {
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            //调用多态程序接口   父类指针指向子类对象
            this->m_empArray[i]->showInfo();
        }
    }
    return;
}

void workerManager::Del_Emp()
{
    //判断文件是否为空
    if (this->m_FileIsEmpty == true)
    {
        cout << "文件为空或者不存在" << endl;
        return;
    }
    else
    {
        //根据职工编号删除职工
        int id = 0;
        cout << "请输入想要删除的员工编号" << endl;
        cin >> id;

        int index = this->IsExist(id);

        //判断职工是否存在
        if (index == -1)
        {
            cout << "没有找到该职工,删除失败" << endl;
        }
        else
        {
            for (int i = index; i < this->m_EmpNum - 1; i++)
            {
                //将数组中的元素向前移动
                this->m_empArray[i] = this->m_empArray[i + 1];
            }
            this->m_EmpNum--; //跟新数组中元素个数

            this->save();

            cout << "删除成功" << endl;
        }
    }
    return;
}

int workerManager::IsExist(int id)
{
    int index = -1;
    //遍历职工数组
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        if (this->m_empArray[i]->m_ID == id)
        {
            index = i;
            break;
        }
    }
    return index;
}

void workerManager::Mod_Emp()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空" << endl;
    }
    else
    {
        cout << "请输入想要修改的员工编号" << endl;
        int id;
        cin >> id;

        //判断员工是否存在
        int ret = this->IsExist(id);

        if (ret == -1)
        {
            cout << "修改失败,查无此人" << endl;
        }
        else
        {
            int id;
            string name;
            int did;

            cout << "该员工存在,请按照提示输入修改信息" << endl;
            cout << "请输入新的编号:" << endl;
            cin >> id;
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                int old_id = this->m_empArray[i]->m_ID;
                if (id == old_id)
                {
                    cout << "此ID已经存在,请输入新的ID" << endl;
                    cin >> id;
                }
            }
            cout << "请输入新的姓名:" << endl;
            cin >> name;

            cout << "请输入新的部门编号" << endl;
            cin >> did;

            Worker *worker = NULL;
            switch (did)
            {
            case 1:
                worker = new Employee(id, name, did);
                break;
            case 2:
                worker = new Manager(id, name, did);
                break;
            case 3:
                worker = new Boss(id, name, did);
                break;
            default:
                break;
            }
            //更新数组元素
            this->m_empArray[ret] = worker;
            cout << "保存成功!" << endl;
            this->save();
        }
    }
    return;
}
void workerManager::Find_Emp()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "请输入查找的方式：" << endl;
        cout << "1.按照职工编号查找" << endl;
        cout << "2.按照职工姓名查找" << endl;
        int select = 0;
        cin >> select;

        if (select == 1)
        {
            int id;
            cout << "请输入要查找的职工编号：" << endl;
            cin >> id;

            int ret = IsExist(id);
            if (ret != -1)
            {
                cout << "查找成功！该职工信息如下：" << endl;
                this->m_empArray[ret]->showInfo();
            }
            else
            {
                cout << "查找失败，查无此人" << endl;
            }
        }
        else if (select == 2)
        {
            string name;
            cout << "请输入要查找的职工姓名" << endl;
            cin >> name;

            bool flag = false; //查找到的标志
            for (int i = 0; i < m_EmpNum; i++)
            {
                if (this->m_empArray[i]->m_Name == name)
                {
                    cout << "查找成功，职工编号为："
                         << m_empArray[i]->m_ID
                         << "号职工的信息如下：" << endl;
                    flag = true;
                    this->m_empArray[i]->showInfo();
                }
            }
            if (flag == false)
            {
                //查无此人
                cout << "查找失败，查无此人" << endl;
            }
        }
        else
        {
            cout << "输入的选项有误" << endl;
        }
    }
    return;
}

void workerManager::Sort_Emp()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空" << endl;
        return;
    }
    else
    {
        cout << "请选择排序方式:" << endl;
        cout << "1.按职工工号进行升序" << endl;
        cout << "2.按职工工号进行降序" << endl;
        int select = 0;
        cin >> select;

        //进行排序
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            int minOrMax = i;
            for (int j = i + 1; j < this->m_EmpNum; i++)
            {
                if (select == 1) //升序排列
                {
                    if (this->m_empArray[minOrMax]->m_ID > this->m_empArray[j]->m_ID)
                    {
                        minOrMax = j;
                    }
                }
                else //降序排列
                {
                    if (this->m_empArray[minOrMax]->m_ID < this->m_empArray[j]->m_ID)
                    {
                        minOrMax = j;
                    }
                }
            }
            if (minOrMax != i)
            {
                Worker *tem = this->m_empArray[i];
                m_empArray[i] = m_empArray[minOrMax];
                m_empArray[minOrMax] = tem;
            }
        }
        cout << "排序成功,排序后的结果如下:" << endl;
        this->save();
        this->show_Emp();
    }
}

void workerManager::Clean_File()
{

    cout << "确认清空？" << endl;
    cout << "1.确认" << endl;
    cout << "2.返回" << endl;
    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //打开模式，ios::trunc 如果存在 删除文件并重新创建
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();

        if (this->m_empArray != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                if (this->m_empArray[i] != NULL)
                {
                    delete this->m_empArray[i];
                }
            }
            this->m_EmpNum = 0;
            delete[] this->m_empArray;
            this->m_empArray = NULL;
            this->m_FileIsEmpty = true;
        }
        cout << "清空成功！" << endl;
    }
}

//析构函数
workerManager::~workerManager()
{
    if (this->m_empArray != NULL)
    {
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            if (this->m_empArray[i] != NULL)
            {
                delete this->m_empArray[i];
            }
        }

        delete[] this->m_empArray;
        this->m_empArray = NULL;
    }
}
